package com.haier.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haier.dto.HpAreaDto;
import com.haier.entity.HpArea;
import com.haier.mapper.HpAreaAuditorMapper;
import com.haier.mapper.HpAreaFreeTimeMapper;
import com.haier.mapper.HpAreaMapper;
import com.haier.service.IHpAreaService;
import com.haier.utils.BeanUtil;
import com.haier.utils.CodeUtils;
import com.haier.utils.EmptyUtils;
import com.haier.vo.ResponseVo;

/**
 * <p>
 * 区域 服务实现类
 * </p>
 *
 * @author luanxuecheng
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class HpAreaServiceImpl extends ServiceImpl<HpAreaMapper, HpArea> implements IHpAreaService {
	@Autowired
	private HpAreaAuditorMapper hpAreaAuditorMapper;
	@Autowired
	private HpAreaFreeTimeMapper hpAreaFreeTimeMapper;

	@Override
	public ResponseVo edit(HpAreaDto dto) {
		HpArea bean = new HpArea();
		BeanUtil.copyNotNullBean(dto, bean);
		if (EmptyUtils.isEmpty(dto.getId())) {
			if (EmptyUtils.isEmpty(dto.getGardenId())) {
				return ResponseVo.checkError("园区id不能为空");
			}
			bean.setCode(CodeUtils.getRandomCode());
			baseMapper.insert(bean);
		} else {
			bean.setCode(null);
			baseMapper.updateById(bean);
		}
		JSONObject result = new JSONObject();
		result.put("id", bean.getId());
		return ResponseVo.success(result);
	}

	@SuppressWarnings(value = { "rawtypes", "unchecked" })
	@Override
	public ResponseVo remove(HpAreaDto dto) {
		Long id = dto.getId();
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("area_id", id);
		hpAreaAuditorMapper.delete(queryWrapper);
		queryWrapper = new QueryWrapper();
		queryWrapper.eq("area_id", id);
		hpAreaFreeTimeMapper.delete(queryWrapper);
		HpArea bean = baseMapper.selectById(dto.getId());
		if (EmptyUtils.isEmpty(bean)) {
			return ResponseVo.checkError("id无效");
		}
		bean.setIsDel(true);
		baseMapper.updateById(bean);
		return ResponseVo.success();
	}

}
